
# Load Libraries
library(tidyr)
library(dplyr)
library(ggplot2)
library(ggsignif)
library(hrbrthemes)


# Read responses
mydata.img.final <- read.csv(file="data/study_2/responses.csv",head=T,sep=c(","))

# Read features
features <- read.csv(file="data/features_labels_wide.csv",head=T,sep=c(","))
truth <- features[,c("file_id","escaped")]
truth$file_id <- as.character(truth$file_id)
mydata.img.final.truth <-
  merge(mydata.img.final,
        truth,
        by.x = c("Img"),
        by.y = c("file_id"))

# Calculate accuracy manually
mydata.img.final.truth$Guessed_Right <- as.numeric(mydata.img.final.truth$Esc == mydata.img.final.truth$escaped)

# Aggregate per user
mydata.analysis <- mydata.img.final.truth %>% group_by(Condition, TurkerID) %>% summarise(c = length(Guessed_Right),m = mean(Guessed_Right))

plotdata <- mydata.analysis %>% group_by(Condition) %>% summarise(mm = mean(m), se = sciplot::se(m))
plotdata$Condition <- c("No training", "Training 12", "Training 4", "Training 8")
plotdata <- plotdata[c(1,3,4,2),]
plotdata <- rbind(plotdata, 
                  data.frame(Condition = c("Naive Bayes","Random Forest","Gradient Boosted Trees","Logistic Regression","Linear SVM"), 
                             mm = c(0.66,0.704,0.708,0.713,0.716),
                             se = c(0,0,0,0,0)))
plotdata$Source <- c(rep(c("Human","Machine"),each=4),"Machine")
plotdata$Guesser <- plotdata$Source
plotdata$Guesser[c(5:9)] <- plotdata$Condition[c(5:9)]
plotdata$Condition <- factor(plotdata$Condition,levels = rev(unique(plotdata$Condition)))
plotdata$Source <- factor(plotdata$Source)
plotdata$Guesser <- factor(plotdata$Guesser)
plotdata.C.M <- plotdata


# Checking with best machine 
t.test(mydata.analysis[which(mydata.analysis$Condition=="training12"),]$m, mu=0.716)

colorset <- rev(c("#EE0000FF","#3B499299","#3B4992CC","#3B4992FF",
                  "#008B45FF","#008B45FF","#008B45FF","#008B45FF","#008B45FF"))

plt <- plotdata.C.M %>%
  ggplot(aes(x=Condition,y=mm,fill=Condition), color=Condition) +  
  stat_summary(fun.y=mean,position=position_dodge(),geom="bar",width=0.6)+
  geom_text(aes(label=round(mm,2)),size = 7, position = position_stack(vjust = 0.8),color="white")+
  geom_errorbar(aes(ymin = mm - 1.96*se, ymax=mm + 1.96*se), 
                position=position_dodge(.9), width=.2, size=1, alpha=c(rep(1,4),rep(0,5)))+
  geom_signif(y_position=c(0.8), annotation=c("**"), xmax = c(6),vjust=5.5,hjust=-0.5,
              xmin=c(1) ,size=1.5,textsize=10)+
  xlab("")+
  ylab("\nAccuracy")+
  coord_flip()+
  ylim(0,0.85)+
  theme_bw()+
  scale_color_manual(name="", values=colorset)+
  scale_fill_manual(name="", values=colorset)+
  guides(fill = guide_legend(reverse = TRUE))+
  guides(color = guide_legend(reverse = TRUE))+ 
  theme_ipsum_rc(grid="Y",axis_title_just="m")+
  theme(text=element_text(size=26),legend.position="no",aspect.ratio=1/1,plot.title = element_text(size=28,hjust = 0.5),
        axis.text.y = element_text(size=22,colour = colorset),axis.title.x = element_text(size=26),
        axis.text.x = element_text(size=24,colour="black"),
        legend.text=element_text(size=16),strip.background = element_rect(color="black",fill="white"),
        panel.border = element_rect(colour = "black", fill=NA, size=1.5))


print(plt)

# END